package anims;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class DrawDoubleBuffer {
	public static void main(String[] a) {
		JFrame f = new JFrame();
		f.setSize(600, 600);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		DrawDoubleBuffers ddbs = new DrawDoubleBuffers();
		f.getContentPane().add(ddbs);
		f.setVisible(true);
		ddbs.draw();
	}

}

class DrawDoubleBuffers extends JPanel {
	static int gap = 30;
	int mx, my;
	int w, h;

	public DrawDoubleBuffers() {
		setSize(600, 600);
		Dimension d = getSize();
		w = d.width;
		h = d.height;

	}

	public void draw() {
		for (int i = 0; i < 1000; i++) {
			gap++;
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			repaint();
		}
	}

	@Override
	protected void paintComponent(Graphics g) {
		Image image = createImage(600, 600);
		Graphics gr = image.getGraphics();

		gr.setColor(Color.blue);
		gr.fillRect(0, 0, w, h);

		gr.setColor(Color.red);
		for (int i = 0; i < w; i += gap)
			gr.drawLine(i, 0, w - i, h);
		for (int i = 0; i < h; i += gap)
			gr.drawLine(0, i, w, h - i);
		System.out.println(image.getWidth(this));;
		g.drawImage(image, image.getWidth(this), image.getHeight(this), null);
	}
}